import { logger } from './logger';
import { promiseRetry } from './retry';
import axios, { AxiosResponse, AxiosRequestConfig } from 'axios';

export async function httpRequest<T>(options: AxiosRequestConfig) : Promise<T> {
  let res!: AxiosResponse<T>;
  await promiseRetry(async (retry, times: number) => {
    try {
      res = await axios.request(options);
    } catch (ex) {
      logger.error('http request error is : \n%O', ex);
      logger.info(`retry ${times} times`);
      retry(ex);
    }
  });
  return res.data;
}